
let num = 1,//用在动画上，让用户在第二次点击的时候可以接着上次转动的角度继续转
  lotteryArrLen = 0//放奖品的数组的长度
const app = getApp();
const req = require("../../utils/http.js");
Page({
  data: {
    contShow: true
  },


  onLoad(options) {
    var t = this
    wx.setNavigationBarTitle({
      title:'大转盘',
    })
  
    wx.getStorage({
      key: 'Id',
      success: function (res) {
        let opind = res.data
        t.setData({
          opind,
        })
      },
    })
    // ----转盘显示列表---
    let params={}
    req.wxReq('/coupon/wxBigWheel/selectBigWheelList',"POST",params).then(res => {
      if (res.data.code == 200) {
        let productId = [] //ID
        let awardsConfig = res.data.result.bigWheelDetailList
        let imgUrl = res.data.result.backgroundUrl
        let lottery = []
        for (var i = 0; i < awardsConfig.length; i++) {
          lottery.push({
            index: i,
            name: awardsConfig[i].name,
          })
          lotteryArrLen = lottery.length;
          t.setData({
            imgUrl: imgUrl,
            lottery: lottery,
            productId: productId //id
          })
        }

      } else {
        wx.showToast({
          title: '敬请期待 ！！!',
          icon: 'none',
          duration: 2000
        })
        t.setData({
          contShow: false //页面内容不显示
        })

      }
    });
    let aniData = wx.createAnimation({
      timingFunction: 'ease-in-out',
      duration: 4000, //持续时间
      transformOrigin: '50% 0 50%'

    })
    t.aniData = aniData; //将动画对象赋值给this的aniData属性

  },

  startRollTap() { //开始转盘
    let that = this;
    let lottery = that.data.lottery
    let productId = that.data.productId //后台返回的数组ID
    // let opind = that.data.opind
    let rand = Math.floor(Math.random() * productId.length)
    let params = {}
    // ---调用接口----
    req.wxReq('/coupon/wxBigWheel/runBigWheel',"GET",params).then(res => {

      if (res.data.code == 200) {
        let list = res.data.result
        that.setData({
          newlist: list,
          // isShow: true,//显示中奖与否  
        })

        // ----如果抽奖次数剩余就转动------
        let angle = res.data.result.angle //后台返回的旋转度数
        let aniData = that.aniData; //获取this对象上的动画对象
        aniData.rotate(-(360 * 2 + angle)).step();
        that.setData({
          aniData: aniData.export()
        })
        num++;
        // ----抽中奖品---
        if (list.objectId !== 0) {
          let objectType = list.objectType
          let productId = list.objectId
          let type='3'
          let setType = '1'
          const params = {
            productId,
          }
          if (objectType==0){ //优惠卷
            req.wxReq('/coupon/wxProduct/receiveCoupon', "GET", params).then(res => {
              if (res.data.code == 200) {
                setTimeout(() => {
                  wx.showModal({
                    content: '奖品已发放到卷包！',
                    showCancel: false
                  })
                }, 3000)

              } else{
                setTimeout(()=>{
                  wx.showToast({
                    title: res.data.message,
                    icon: 'none',
                    duration: 1500
                  })  
                },3000)

              }
            })
          } else if (objectType == 1){ //抽中的商品
            setTimeout(() => {
              wx.showToast({
                title: '恭喜您抽中了' + list.name,
                icon: 'none',
                duration: 1800
              })
            }, 3000)
            
            setTimeout(()=>{
              wx.navigateTo({
                url: '../product/index?productId=' + productId + '&type=' + type + '&setType=' + setType,
              })
            },3800)
          
          }
       
      
        } else { //未抽中奖品
          setTimeout(function () {
            that.setData({
              isShowModel: true
            })
          }, 4000)
          setTimeout(function () {
            that.setData({
              isShowModel: false
            })
          }, 4800)

        }
        setTimeout(() => {
          that.reset()
        }, 5000)
      } else { //无法转动，直接弹框
        // --弹框----
        setTimeout(function () {
          that.setData({
            times: res.data.message,
            isShowModel: true
          })
        }, 500)
        setTimeout(function () {
          that.setData({
            times: res.data.message,
            isShowModel: false
          })
        }, 1500)
      }
    });

  },
  // --还原--
  reset: function () {
    this.aniData.rotate(0, 0)
      .scale(1)
      .translate(0, 0)
      .skew(0, 0)
      .step({ duration: 0 })
    this.setData({ aniData: this.aniData.export() })
  },

})

